<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="用户ID" prop="userId">
        <el-input v-model="queryParams.userId" placeholder="请输入用户ID" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户账号" prop="account">
        <el-input v-model="queryParams.account" placeholder="请输入用户账号" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="任务ID" prop="taskId">
        <el-input v-model="queryParams.taskId" placeholder="请输入任务ID" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="任务名称" prop="taskName">
        <el-input v-model="queryParams.taskName" placeholder="请输入任务名称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="任务时间">
        <el-date-picker v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置
        </el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="UserUploadTaskList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="用户账号" align="center" prop="account" />
      <el-table-column label="任务ID" align="center" prop="taskId">
      </el-table-column>
      <el-table-column label="任务名称" align="center" prop="taskName" />
      <el-table-column label="任务奖励" align="center" prop="rewardAmt" />

      <el-table-column label="任务时间" align="center" prop="createTime">
      </el-table-column>

    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body @close="cancel">
      <el-form ref="formRef" :model="form" label-width="80px">
        <el-form-item label="" prop="remark" :rules="{
          required: true,
          message: '请输入备注',
          trigger: 'blur',
        }">
          <!-- v-model="textarea" -->
          <el-input type="textarea" placeholder="请输入内容" maxlength="150" v-model="form.remark" show-word-limit
            :autosize="{ minRows: 6, maxRows: 6 }">
          </el-input>
        </el-form-item>

        <!-- <el-form-item label="发布者ID">
          <span>{{ form.userId }}</span>
        </el-form-item>
        -->
        <el-form-item>
          <el-button type="primary" @click="submit">{{ handBt }}</el-button>
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {
  getlistUserTask,
  UserTaskAudit,
  submitError,
} from "@/api/admin/userTask";
export default {
  name: "taskManage",
  data () {
    return {
      // 显示搜索条件
      showSearch: true,
      requestUrl: this.$img.requestUrl,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: "",
        account: "",
        taskId: "",
        taskName: "",

      },
      // 总条数
      total: 0,
      loading: false,
      UserUploadTaskList: [],
      dateRange: [],
      form: {},
      open: false,
      title: "",
      handBt: "",
      resource: "1",
      orderStatusOptions: [
        {
          dictValue: 0,
          dictLabel: "待审核",
        },
        {
          dictValue: 1,
          dictLabel: "通过审核",
        },
        {
          dictValue: -1,
          dictLabel: "拒绝审核",
        },
      ],
    };
  },
  created () {
    this.getList();
  },

  methods: {
    getList () {
      this.loading = true;
      getlistUserTask(
        this.addDateRange(this.queryParams, this.dateRange)

      ).then((response) => {
        // response.rows.forEach((item, index) => {
        //   item.detailImgList.forEach((item2, index) => {
        //     item.detailImgList[index] = this.requestUrl + item2;
        //   });
        // });
        this.UserUploadTaskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 类型字典翻译
    appTypeFormat (row, column) {
      return this.selectDictLabel(this.orderStatusOptions, row.auditStatus);
    },

    audit (row, index) {
      let self = this;

      this.$confirm(
        "是否确认" +
        (index == 1 ? "通过" : "拒绝") +
        "《ID:" +
        row.userId +
        "》" +
        row.taskName +
        "审核?",
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          UserTaskAudit({
            id: row.id,
            auditStatus: parseInt(index),
            remark: "",
          }).then((response) => {
            self.msgSuccess(index == 1 ? "已通过" : "已拒绝");
            self.getList();
          });
        })
        .catch(function () { });
    },

    //审核
    submit () {
      let self = this;
      this.$refs["formRef"].validate((valid) => {
        if (valid) {
          if (self.form.auditStatus === 1) {
            submitError({
              id: self.form.id,
              remark: self.form.remark,
            }).then((response) => {
              if (response.code == 200) {
                self.msgSuccess("操作成功");
                self.getList();
              }
            });
          } else {
            UserTaskAudit({
              id: self.form.id,
              auditStatus: -1,
              remark: self.form.remark,
            }).then((response) => {
              if (response.code == 200) {
                self.msgSuccess("操作成功");
                self.getList();
              }
            });
          }
          this.open = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    // 取消按钮
    cancel () {
      this.open = false; //地址弹窗
    },

    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.dateRange = [];
      this.handleQuery();
    },
    handleView (row, index) {
      this.form = row;
      if (index == -1) {
        this.title = "审核拒绝";
        this.handBt = "拒绝";
      } else {
        this.title = "账号异常";
        this.handBt = "确定";
      }

      this.open = true;
    },
  },
};
</script>
<style scoped lang="scss"></style>
